package com.registration.service;

import com.registration.bean.CodeBean;
import com.registration.bean.UserBean;
import com.registration.mapper.UserMapper;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    //账号密码登录
    public Integer loginByPassword(String phone, String password) {

        Integer result;
        BCrypt bcrypt = new BCrypt();
        UserBean userBean = userMapper.loginByPassword(phone);
        if (userBean == null) {
            throw new RuntimeException("该用户不存在");
        }
        if (userBean.getStatus() == 0) {
            throw new RuntimeException("该用户已被禁用");
        }
        if (userBean.getPassword() == null || password.isEmpty()) {
            throw new RuntimeException("该用户未设置密码");
        }
        if (bcrypt.checkpw(password, userBean.getPassword())) {
            result = 0;
        } else {
            result = 1;
        }
        lastLoginTime(phone);
        return result;
    }

    @Override
    //验证码登录
    public CodeBean loginByCode(String phone, Integer code) {
        if (validCode(phone, code)) {
            UserBean userBean = userMapper.selectUserByPhone(phone);
            if (userBean == null) {
                return new CodeBean(0);
            } else {
                if (userBean.getStatus() == 0) {
                    throw new RuntimeException("该用户已被禁用");
                } else {
                    userBean.setCode(code);
                    vitiateCode(phone);
                    lastLoginTime(phone);
                    return userMapper.loginByCode(phone, code);
                }
            }

        } else {
            throw new RuntimeException("验证码不匹配");
        }
    }

    @Override
    //生成验证码，插入数据库并发送验证码
    public Integer sendCode(String phone) {
        Random random = new Random();
        Integer code = random.nextInt(900000) + 100000;
        vitiateCode(phone);
        userMapper.insertCode(phone, code);
        System.out.println(code);
        return code;
    }

    @Override
    //验证码失效
    public void vitiateCode(String phone) {
        userMapper.vitiateCode(phone);
    }

    @Override
    public void invalidCode(Integer timeoutMinutes) {
        userMapper.invalidCode(timeoutMinutes);
    }

    @Override
    //注册
    public Integer register(UserBean userBean) {
        Integer result = 1;
        BCrypt bcrypt = new BCrypt();
        if (userBean.getPassword() != null && !userBean.getPassword().isEmpty()) {
            userBean.setPassword(bcrypt.hashpw(userBean.getPassword(), BCrypt.gensalt()));
        }
        if (validCode(userBean.getPhone(), userBean.getCode())) {
            if (userMapper.selectUserByPhone(userBean.getPhone()) == null) {
                result = (userMapper.register(userBean)==1) ? 0 : 1;
                vitiateCode(userBean.getPhone());
            } else {
                result = 2;//2为账号已存在
            }
        }
        return result;
    }

    @Override
    //验证验证码可用性
    public Boolean validCode(String phone, Integer code) {
        Integer result = userMapper.validCode(phone, code);
        return result != null && result == 1;
    }

    @Override
    //重置密码
    public Integer resetPassword(String phone, String password, Integer code) {
        if (validCode(phone, code)) {
            BCrypt bcrypt = new BCrypt();
            password = bcrypt.hashpw(password, BCrypt.gensalt());
            userMapper.resetPassword(phone, password);
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    //记录最后一次登录时间
    public void lastLoginTime(String phone) {
        userMapper.lastLoginTime(phone);
    }

    @Override
    //更新用户信息
    public Integer updateUser(UserBean userBean) {
        return userMapper.updateUser(userBean);
    }

    @Override
    //修改手机号
    public Integer changePhone(UserBean userBean) {
        Integer result;
        if (validCode(userBean.getPhone(), userBean.getCode())) {
            userMapper.vitiateCode(userBean.getPhone());
            UserBean u = userMapper.selectUserByPhone(userBean.getPhone());
            // 添加空值检查
            if (u == null) {
                // 如果通过新手机号查询不到用户，说明是正常更换手机号的情况
                result = (userMapper.changePhone(userBean)==1)?0:1;
            } else {
                // 如果通过新手机号能查询到用户，检查是否是同一用户
                if (!u.getUser_id().equals(userBean.getUser_id())) {
                    result = 2; // 手机号已被其他用户使用
                } else {
                    result = (userMapper.changePhone(userBean)==1)?0:1; // 同一用户，可以更新
                }
            }
        } else {
            result = 1; // 验证码无效
        }
        return result;
    }

    @Override
    public UserBean selectUserByPhone(String phone) {
        return userMapper.selectUserByPhone(phone);
    }
}

